package com.itic.develop.weeks.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.applications.sys.user.persistence.model.SysUser;
import com.itic.appbase.applications.sys.user.service.SysUserService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;
import com.itic.develop.weeks.persistence.model.Weeks;
import com.itic.develop.weeks.persistence.model.WeeksExample;
import com.itic.develop.weeks.persistence.model.WeeksExample.Criteria;
import com.itic.develop.weeks.service.WeeksService;


@Controller
@RequestMapping(value="/develop/weeksview")
public class WeeksViewController
     extends BaseController {
    @Autowired
    private WeeksService weeksService;

    @Autowired
    private SysUserService sysUserService;
    
    @Autowired
	protected OutCommonService outCommonService;
    
    private static final String DEVELOP_ORG_CODE = "00076875013";
    
    
    //////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
    private static final String MODEL_NAME_CHINA = "日志查看";//中文名
    private static final String MODEL_NAME＿ENGLISH = "weeksview";//英文名
    private static final String PAGE_COMMON_PATH = "/develop/weeksview/weeksview";//通用路径
    private static final String COMMON_URL = "/develop/weeksview/";//通用路径
    
    // 返回属性黑名单
    private static String[] ignoreProperties = new String[] { "isEffective", "createTime", "updateTime","orgCode" };


    //////////////////////////////////////////////////////页面跳转区////////////////////////////////////////////////////////////
   
    /*
     * 主页面
     */
    @RequestMapping(value="/init")
    public String init(Model model){
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
    	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	model.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"Main";
    }
    
    /*
     * 查看页面
     */
    @RequestMapping(value="/initView")
    public String initView(String id , Model model){
    	if( StringHelper.isNotBlank(id) ) {
    		
    		Weeks temp  = weeksService.get(id);
    		temp.setCreateUser(sysUserService.get(temp.getCreateUser()).getRealname());
    		
    		model.addAttribute("viewEdit",iticJsonMapper.toJson(temp));
    		model.addAttribute("name_china", MODEL_NAME_CHINA);
        	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
        	model.addAttribute("common_url", COMMON_URL);
    	}else {
            throw new ParameterErrorException();
        }
    	
    	return PAGE_COMMON_PATH+"View";
    }
    
    
    //////////////////////////////////////////////////////业务代码区////////////////////////////////////////////////////////////
    
    
    
    /**
     * <p>Title:queryCondition </p>
     * <p>Description:封装查询条件 </p>
     * @param jzdw
     * @param jzdwExample
     * @return
     */
    private WeeksExample queryCondition(HttpServletRequest request,Weeks temp) {
    	
    	// 参数处理
        WeeksExample example = new WeeksExample();
        // 按创建时间倒序
        example.setOrderByClause("CREATE_TIME DESC");
        
        Criteria criteria = example.createCriteria().andIsEffectiveEqualTo( DBConst.TRUE );
      
        // 查询条件
        if ( null != temp ) {
    	    //模糊查询
            if ( StringHelper.isNotBlank( temp.getCreateUser() ) ) {
                criteria.andCreateUserLike("%" + temp.getCreateUser() + "%");
            }
        }
        return example;
    }
    
    /**
     * 
     * @param goldType
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(Weeks temp, HttpServletRequest request){
        // 声明返回对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        
        WeeksExample example = this.queryCondition(request,temp);
        // 封装分页参数
        example = this.handlePageQuery(request, example);
        
        
        // 获取信息总条数
        int total = weeksService.count(example);
        
        // 获取数据
        List<Weeks> list = weeksService.list(example);
        
        //结果集
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        if( list.size() > 0 ){
        	for( Weeks tmp : list){
        		tmp.setCreateUser(sysUserService.get(tmp.getCreateUser()).getRealname());
        	}
            //过滤黑名单字段   
        	resultList = ValueHelper.tranList2MapList(list, ignoreProperties);
        }
        
        // 数据返回处理
        resultMap.put(IticConst.TOTAL_RECORDS, total);
        resultMap.put(IticConst.DISPLAY_RECORDS, total);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, resultList);
        return resultMap;

    }
    

    /**
   	 * 删除
   	 * 
   	 */
   	@RequestMapping(value = "/sysUserList")
   	@ResponseBody
   	public List<SysUser> sysUserList() {
   		return outCommonService.getSysUserList(DEVELOP_ORG_CODE);
   	}
}